package com.zr.plant.service.impl;

import com.zr.meiju.StatusEnum;
import com.zr.meiju.StatusValueEnum;
import com.zr.plant.mapper.PlantMapper;
import com.zr.plant.pojo.*;
import com.zr.plant.service.PlantService;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by 86151 on 2019/6/14.
 */
@Service
@Slf4j
public class PlantServiceImpl implements PlantService {
    @Autowired
    private PlantMapper plantMapper;

    @Override
    @Transactional
    public ResultVo addPlant(PlantAddVo plantAddVo) {
        //逻辑验证
        ResultVo assigment = assigment(plantAddVo);
        if (!assigment.isSuccess()){
            return assigment;
        }
        //给数据库中的创建时间和修改时间等信息赋值
        PlantClass plantClass = new PlantClass();
        Date nowDate = new Date();
        plantClass.setCreateTime(nowDate);
        plantClass.setCreateName("小明");
        plantClass.setUpdateTime(nowDate);
        plantClass.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(plantAddVo, plantClass);
        plantMapper.addPlant(plantClass);
        return ResultBuildVo.success(plantAddVo);
    }
    @Transactional
    public ResultVo updatePlant(PlantUpdateVo plantUpdateVo) {
        //逻辑验证=禁用状态下才允许修改
        ResultVo assigment = assigmentUpdate(plantUpdateVo);
        if (!assigment.isSuccess()){
            return assigment;
        }
        //给数据库中的创建时间和修改时间等信息赋值
        PlantClass plantClass = new PlantClass();
        Date nowDate = new Date();
        plantClass.setUpdateTime(nowDate);
        plantClass.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(plantUpdateVo, plantClass);
        plantMapper.updatePlant(plantClass);
        return ResultBuildVo.success(plantUpdateVo);
    }

    @Transactional
    public ResultVo updateStatus(PlantStatusVo plantStatusVo) {
        //逻辑验证
        //验证数据是否存在
        PlantClass queryPlantById = plantMapper.queryPlantById(plantStatusVo.getId());
        if (queryPlantById==null){
            return ResultBuildVo.error("修改数据不存在！","500");
        }
        //给数据库中的创建时间和修改时间等信息赋值
        PlantClass plantClass = new PlantClass();
        Date nowDate = new Date();
        plantClass.setUpdateTime(nowDate);
        plantClass.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(plantStatusVo, plantClass);
        plantMapper.updateStatus(plantClass);
        return ResultBuildVo.success(plantClass);
    }

    public ResultVo assigmentUpdate(PlantUpdateVo plantUpdateVo){
        //验证数据是否存在
        PlantClass plantClass = plantMapper.queryPlantById(plantUpdateVo.getId());
        if (plantClass==null){
            return ResultBuildVo.error("修改数据不存在！","500");
        }
        //禁用状态下才允许修改
        if (plantClass.getStatus()== StatusEnum.QIYONG.getStatusValue()){
            return ResultBuildVo.error("禁用状态才能修改！","500");
        }
        return ResultBuildVo.success(plantUpdateVo);
    }

    @Override
    public ResultVo<List<Legalperson>> queryDown(Integer uid) {
        if (uid==null){
            return ResultBuildVo.error("查询时ID不能为空！","500");
        }
        List<Legalperson> legalpersonList = plantMapper.queryLegalPersonByUid(uid);
        return ResultBuildVo.success(legalpersonList);
    }

    @Override
    public ResultVo<PlantClass> queryPlantById(Integer id) {
        PlantClass queryPlantById = plantMapper.queryPlantById(id);
        if (queryPlantById==null){
            return ResultBuildVo.error("查看数据不存在！","500");
        }
        return ResultBuildVo.success(queryPlantById);
    }

    @Override
    public ResultVo<AllRecords> queryPage(PlantSelectVo plantSelectVo) {
        //查询数据
        List<PlantClass> plantClassList = plantMapper.queryPage(plantSelectVo);
        for(PlantClass plantClass:plantClassList){
           /* if (plantClass.getStatus()==StatusEnum.QIYONG.getStatusValue()){
                plantClass.setStatusName(StatusEnum.QIYONG.getStatusName());
            }
            if (plantClass.getStatus()==StatusEnum.JINYONG.getStatusValue()){
                plantClass.setStatusName(StatusEnum.JINYONG.getStatusName());
            }*/
            plantClass.setStatusName(StatusEnum.getName(plantClass.getStatus()));

            /*for (StatusEnum c : StatusEnum.values()) {
                if (c.getStatusValue() == plantClass.getStatus()) {
                    plantClass.setStatusName( c.getStatusName());
                }
            }*/
        }
        //查询数量
        int count =  plantMapper.queryCount(plantSelectVo);
        //把当前页、每页大小、总页数、总条数、数据统一放入到AllRecords返回
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(plantSelectVo.getPageIndex());
        allRecords.setPageSize(plantSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(plantClassList);
        return ResultBuildVo.success(allRecords);
    }

    /* @Override
     public ResultVo<PlantClass> queryPlanClassById(Integer id) {
         if (id==null){
             return ResultBuildVo.error("查看时id不能为空！","500");
         }
         PlantClass plantClass = planClassMapper.queryById(id);
         if (plantClass ==null){
             return ResultBuildVo.error("查看对象不存在！","500");
         }
         return ResultBuildVo.success(plantClass);
     }


 */
    public ResultVo assigment(PlantAddVo plantAddVo){
        //        1.验证工厂编码是否已经存在
        List<PlantClass> plantClassesCode = plantMapper.queryByCode(plantAddVo.getCode());
        if (!CollectionUtils.isEmpty(plantClassesCode)){
            return ResultBuildVo.error("已经存在工厂编码！","500");
        }
//        2.验证法人编码是否存在
        List<Legalperson> legalpersonList = plantMapper.queryLegalPersonByCode(plantAddVo.getLegalPersonCode());
        if (CollectionUtils.isEmpty(legalpersonList)){
            return ResultBuildVo.error("法人信息不存在！","500");
        }
        return ResultBuildVo.success(plantAddVo);
    }
}
